package com.leetcode.dynamic_programming;

import java.util.List;

/**
 * @author Dennis Li
 * @date 2020/7/20 20:46
 */
public class WordBreak_139 {

    public boolean wordBreak(String s, List<String> wordDict) {
        int len = s.length();
        boolean[] dp = new boolean[len + 1];
        dp[0] = true;
        // 完全背包问题，对于背包的迭代应该放在内层
        for (int i = 0; i < len; i++) {
            for (String word : wordDict) {
                int length = word.length();
                // 迭代其子串，直到能够对所有查找到
                if (length <= i && word.equals(s.substring(i - length, i))) {
                    dp[i] = dp[i] || dp[i - length];
                }
            }
        }
        return dp[len];
    }

}
